#include "Stdafx.h"
#include "JassAccelerator.h"
#include "Global_variable.h"

#include "Pascal.hpp"
#include "comparators.hpp"
#include "data.hpp"
#include "handletables.hpp"
#include "myjassvm.hpp"
#include "pool.hpp"
#include "wc3_game_state.hpp"
#include "wc3string.hpp"
#include "wc3collections.hpp"
#include "wc3jassvm.hpp"
#include "jassvm_extension.hpp"

#include "wc3stringhash.hpp"
#include "wc3refcnt.hpp"
#include "wc3props.hpp"
#include "wc3observer.hpp"
#include "wc3memory.hpp"
#include "wc3main.hpp"
#include "wc3game.hpp"
#include "wc3float.hpp"
#include "wc3agent.hpp"
#include "wc3_native_signature_parser.hpp"
#include "sharedmemory.hpp"
#include "logging.hpp"
#include "jassvmhooks.hpp"
#include "hooks.hpp"
#include "DebugUtils.hpp"
#include "debug_logger.hpp"


/*exports
	SetTransformJassCodeHandleFunction,
	GetTransformJassCodeHandleFunction,
	ResetTransformJassCodeHandleFunction;
	*/
Boolean PublishModulePath(HMODULE hModule, String Key)
{
	String UniqueName;
	Handle hStorage;
	char ModuleFileName[200];

	UniqueName = new char[200];
	GetModuleFileNameA(hModule,(LPSTR)ModuleFileName,200);
	sprintf(UniqueName,"%s.library_path.%d", Key, GetCurrentProcessId());
	return CreateSharedConstantString(
		UniqueName,
		ModuleFileName,
		hStorage
	);
};

/*struct Params {
    DWORD ProcessID;
    std::list<HWND> WindowHandle;
    Params(DWORD pid) : ProcessID(pid) {}
};
 
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
    Params *pParam = (Params *)lParam;
    DWORD dwProcessID = 0;
 
    GetWindowThreadProcessId(hWnd, &dwProcessID);
 
    if (dwProcessID == pParam->ProcessID)
        pParam->WindowHandle.push_back(hWnd);
 
    return TRUE;
}



DWORD PIDByName(WCHAR * AProcessName)
{
  HANDLE pHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  PROCESSENTRY32 ProcessEntry;
  DWORD pid;
  ProcessEntry.dwSize = sizeof(ProcessEntry);
  bool Loop = Process32First(pHandle, &ProcessEntry);
 
  while (Loop)
    {
      if (wcsstr(ProcessEntry.szExeFile, AProcessName))
      {
          pid = ProcessEntry.th32ProcessID;
          CloseHandle(pHandle);
          return pid;
      }
      Loop = Process32Next(pHandle, &ProcessEntry);
	  //fwrite(&ProcessEntry.szExeFile,1,sizeof(ProcessEntry.szExeFile),hFile);
    }
	//fclose(hFile);	// закрываем файл
    return 0;
}*/

void main()
{
	//debug_logger_main();
	jassvm_extension_main();
	jassvmhooks_main();
	wc3agent_main();
	wc3game_main();
	wc3jassvm_main();
	wc3observer_main();
	wc3props_main();
	wc3refcnt_main();
	wc3string_main();
};

BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{	
	if (ul_reason_for_call != DLL_PROCESS_ATTACH) return TRUE;
	DisableThreadLibraryCalls(hModule);

	//Params Param(PIDByName(L"war3.exe"));
    //EnumWindows(EnumWindowsProc, (LPARAM)&Param);
	//MessageBoxA(*Param.WindowHandle.begin(),"Start DLL!.","Start DLL!",MB_OK | MB_ICONWARNING);
	MessageBoxA(NULL,"Start DLL!.","Start DLL!",MB_OK | MB_ICONWARNING);
	int v1 = 1;
	if (v1 == 1) return TRUE;

	/*HMODULE hDll;       // handle to existing .EXE file
	hDll = LoadLibrary(TEXT("JassAccelerator.dll"));
	if (hDll == NULL)
	{
		MessageBoxA(NULL,"Could not load dll.","Error!",MB_OK | MB_ICONWARNING);
		return TRUE;
	}*/
	PublishModulePath(hModule, "xgm.icefog.jass_accelerator");
	wc3main_initialization();
	myjassvm_initialization();
	main();
	wc3main_Init(GetModuleHandleA("game.dll"));
	pOnInstanceCreated = &OnInstanceCreated;
	pOnInstanceDestroying = &OnInstanceDestroying;
	pOnInstanceInit = &OnInstanceInit;
	pOnExecuteFunctionByName = &OnInstanceExecuteFunctionByName;
	pOnExecuteFunctionByHandle = &OnInstanceExecuteFunctionByHandle;
	pOnInstanceGetStringHandle = &OnInstanceGetStringHandle;
	pOnEnableVarChangeEvent = &OnEnableVarChangeEvent;
	wc3main_finalization();
	myjassvm_finalization();
	return TRUE;
}

void fTest()
{
	FILE* hFile;
	char vBuf[1000];
	char FullPath[100];
	sprintf(FullPath,"%s\0","C:\\Games\\123.txt");
	sprintf(vBuf,"test111!");
	hFile = fopen(FullPath,"ab");	// открыть файл
	if (hFile == NULL)
		return;
	fwrite(&vBuf,1,strlen(vBuf),hFile);
	fclose(hFile);
}